package com.magist.manage.system.controller;

import com.github.pagehelper.PageInfo;
import com.magist.core.controller.BaseController;
import com.magist.core.entity.ResultMessage;
import com.magist.core.exception.BusinessException;
import com.magist.core.util.DateUtil;
import com.magist.manage.enums.ContactStatusEnum;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import com.magist.manage.system.entity.OffContact;
import com.magist.manage.system.dto.OffContactDto;
import com.magist.manage.system.service.OffContactService;

@Api(value = "RestOffContactController", description = "RestOffContactController接口")
@RestController
@RequestMapping("/rest/offContact")
public class RestOffContactController extends BaseController {

	@Autowired
	private OffContactService offContactService;

	@ApiOperation(value="保存", notes="保存接口")
	@RequestMapping(value = "save.do", method = RequestMethod.POST)
	public Object save(@ModelAttribute("offContactDto") OffContactDto offContactDto) {
		OffContact offContact = new OffContact();
		BeanUtils.copyProperties(offContactDto,offContact);
		// 时间转换
		offContact.setLinkDate(DateUtil.parse(offContactDto.getLinkDate(),DateUtil.yyyy_MM_dd));
		offContact.setId(offContactService.getNewId());
		offContactService.insert(offContact);
		return ResultMessage.success();
	}

	@ApiOperation(value="更新", notes="更新接口")
	@RequestMapping(value = "update.do", method = RequestMethod.POST)
	public Object update(@ModelAttribute("offContactDto") OffContactDto offContactDto) {
		OffContact oldOffContact = offContactService.getById(offContactDto.getId());
		if(oldOffContact==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		OffContact offContact = new OffContact();
		BeanUtils.copyProperties(offContactDto,offContact);
		// 时间转换
		offContact.setLinkDate(DateUtil.parse(offContactDto.getLinkDate(),DateUtil.yyyy_MM_dd));
		offContact.setModifyTime(oldOffContact.getModifyTime());
		if (offContactService.update(offContact)>0) 
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="列表", notes="分页查询接口")
	@RequestMapping(value = "page.do", method = RequestMethod.GET)
	public Object page(@ModelAttribute("offContactDto") OffContactDto offContactDto) {
		ResultMessage result = ResultMessage.success();
		OffContact offContact = new OffContact();
		BeanUtils.copyProperties(offContactDto,offContact);
		// 模糊查询
		String linkTel = offContact.getLinkTel();
		String linkName = offContact.getLinkName();
		if(!StringUtils.isEmpty(linkTel)) {
			offContact.setLinkTel("%" + linkTel+ "%");
		}
		if(!StringUtils.isEmpty(linkName)) {
			offContact.setLinkName("%" + linkName+ "%");
		}
		PageInfo<OffContact> pageInfo = offContactService.page(offContact,getPageInfo());
		result.setContent(pageInfo);
		return result;
	}

	@ApiOperation(value="删除", notes="删除接口")
	@RequestMapping(value = "del.do", method = RequestMethod.POST)
	public Object del(@ModelAttribute("id") String id) {
		OffContact offContact = offContactService.getById(id);
		if(offContact==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		if(offContactService.delete(offContact)>0)
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="详情", notes="详情接口")
	@RequestMapping(value = "info.do", method = RequestMethod.GET)
	public Object info(@ModelAttribute("id") String id) {
		OffContact offContact = offContactService.getById(id);
		if(offContact==null)
			throw new BusinessException("无效的数据!");
		OffContactDto offContactDto = new OffContactDto();
		BeanUtils.copyProperties(offContact,offContactDto);
		ResultMessage result = ResultMessage.success();
		result.setContent(offContactDto);
		return result;
	}

	@ApiOperation(value="详情", notes="详情接口")
	@RequestMapping(value = "remarkTalk.do", method = RequestMethod.POST)
	public Object remarkTalk(String id,String talkRemark) {
		OffContact offContact = offContactService.getById(id);
		if(offContact==null)
			throw new BusinessException("无效的数据!");
		offContact.setStatus(ContactStatusEnum.LINKED.getCode());
		offContact.setTalkRemark(talkRemark);
		offContactService.update(offContact);
		ResultMessage result = ResultMessage.success();
		return result;
	}

}
